package com.ticket.web.controller.yanchu;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.utils.SecurityUtils;
import com.ticket.framework.web.service.TokenService;
import com.ticket.system.domain.vo.StorageAndBatchVO;
import com.ticket.system.domain.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.enums.BusinessType;
import com.ticket.system.domain.YanchuUserInfo;
import com.ticket.system.service.IYanchuUserInfoService;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-10-09
 */
@Api(description = "录入用户管理")
@RestController
@RequestMapping("/system/info")
public class YanchuUserInfoController extends BaseController
{
    @Autowired
    private IYanchuUserInfoService yanchuUserInfoService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询绑定用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(YanchuUserInfo yanchuUserInfo)
    {
        startPage();
        List<YanchuUserInfo> list = yanchuUserInfoService.selectYanchuUserInfoList(yanchuUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出绑定用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "绑定用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuUserInfo yanchuUserInfo)
    {
        List<YanchuUserInfo> list = yanchuUserInfoService.selectYanchuUserInfoList(yanchuUserInfo);
        ExcelUtil<YanchuUserInfo> util = new ExcelUtil<YanchuUserInfo>(YanchuUserInfo.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取绑定用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuUserInfoService.selectYanchuUserInfoById(id));
    }
    @ApiOperation("查询小程序绑定用户")
    @GetMapping("/listInfo")
    public TableDataInfo listInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        YanchuUserInfo yanchuUserInfo = new YanchuUserInfo();
        startPage();
        List<YanchuUserInfo> list = yanchuUserInfoService.selectYanchuUserInfoList(yanchuUserInfo);
        return getDataTable(list);
    }

    /**
     * 新增绑定用户
     */
    @ApiOperation("新增绑定用户")
    @ApiImplicitParam(name = "userInfoVo", value = "绑定用户对象", dataType = "UserInfoVo", dataTypeClass = UserInfoVo.class)
    @Log(title = "绑定用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfoVo userInfoVo)
    {
        try {
            return toAjax(yanchuUserInfoService.insertYanchuUserInfo(userInfoVo));
        }catch (Exception e){
            logger.error("绑定用户失败",e);
            return AjaxResult.error("操作失败！");
        }
    }

    /**
     * 修改绑定用户
     */
    @Log(title = "绑定用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YanchuUserInfo yanchuUserInfo)
    {
        return toAjax(yanchuUserInfoService.updateYanchuUserInfo(yanchuUserInfo));
    }

    /**
     * 删除绑定用户
     */
    @Log(title = "绑定用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuUserInfoService.deleteYanchuUserInfoByIds(ids));
    }

    @ApiOperation("实名认证")
    @ApiImplicitParam(name = "userInfoVo", value = "绑定用户对象", dataType = "UserInfoVo", dataTypeClass = UserInfoVo.class)
    @Log(title = "实名认证", businessType = BusinessType.INSERT)
    @PostMapping("/auth")
    public AjaxResult auth(@Valid UserInfoVo userInfoVo, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        try {
            YanchuUserInfo userInfo = yanchuUserInfoService.authUserInfo(userInfoVo,loginUser);
            if(null != userInfo){
                loginUser.getUser().setIsAuth(1);
                loginUser.getUser().setUserinfoId(userInfo.getId());
                tokenService.setLoginUser(loginUser);
            }
            return AjaxResult.success(userInfo);
        }catch (Exception e){
            logger.error("实名认证失败",e);
            return AjaxResult.error("操作失败！");
        }
    }

    @ApiOperation("实名认证查询")
    @PostMapping("/authQuery")
    public AjaxResult authQuery(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        try {
            return success(yanchuUserInfoService.selectUserInfoByUserId(loginUser.getUser().getUserId()));
        }catch (Exception e){
            logger.error("实名认证失败",e);
            return AjaxResult.error("操作失败！");
        }
    }

    @ApiOperation("集团管理员 会员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "会员名称",dataTypeClass = String.class,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "会员手机号",dataTypeClass = String.class,dataType = "String"),
            @ApiImplicitParam(name = "sex",value = "会员名称",dataTypeClass = String.class,dataType = "String"),
    })
    @GetMapping("/userList")
    public TableDataInfo userList(String userName,String phone,Integer sex){
        startPage();
        return getDataTable(yanchuUserInfoService.getUserList(userName,phone,sex));
    }

    @ApiOperation("小程序获取储值卡余额和可用观演卡数量")
    @ApiImplicitParam(name = "userId",value = "用户id",dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/getStorageAndBatch/forAllUser")
    public AjaxResult getStorageAndBatch(Long userId){
        if (null == userId){
            StorageAndBatchVO vo = new StorageAndBatchVO();
            vo.setStorage("0");
            vo.setBatch(0);
            vo.setScore(0);
            return success(vo);
        }
        return success(yanchuUserInfoService.getStorageAndBatch(userId));
    }
}
